HDU 5636 Shortest Path 暴力
Shortest Path
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5636
Description
There is a path graph G=(V,E) with n vertices. Vertices are numbered from 1 to n and there is an edge with unit length between i and i+1 (1≤i<n). To make the graph more interesting, someone adds three more edges to the graph. The length of each new edge is 1.
You are given the graph and several queries about the shortest path between some pairs of vertices.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line contains two integer n and m (1≤n,m≤105) -- the number of vertices and the number of queries. The next line contains 6 integers a1,b1,a2,b2,a3,b3 (1≤a1,a2,a3,b1,b2,b3≤n), separated by a space, denoting the new added three edges are (a1,b1), (a2,b2), (a3,b3).
In the next m lines, each contains two integers si and ti (1≤si,ti≤n), denoting a query.
The sum of values of m in all test cases doesn't exceed 106.
Output
For each test cases, output an integer S=(∑i=1mi⋅zi) mod (109+7), where zi is the answer for i-th query.
Sample Input
4 61
10 2
2 4 5 7 8 10
1 5
3 1
Sample Output
7
Hint
题意
有一条长度为n的链. 节点i和i+1之间有长度为1的边. 现在又新加了3条边, 每条边长度都是1. 给出m个询问, 每次询问两点之间的最短路.
题解:
暴力枚举每种情况就好了
反正我是真暴力枚举 -.-
你路径要么从起点走到某条边的起点,然后出来,然后再走到某条边的起点,某条边的终点这样。
暴力就好了。
代码
#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[4],b[4];
const int mod = 1e9+7;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
long long Ans = 0;
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=3;i++)
scanf("%d%d",&a[i],&b[i]);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
int ans = abs(y-x);
for(int j=1;j<=3;j++)
{
ans = min(ans,abs(a[j]-x)+abs(b[j]-y)+1);
ans = min(ans,abs(b[j]-x)+abs(a[j]-y)+1);
}
for(int j=1;j<=3;j++)
{
for(int k=1;k<=3;k++)
{
if(j==k)continue;
ans = min(ans,abs(a[j]-x)+abs(a[k]-b[j])+abs(y-b[k])+2);
ans = min(ans,abs(b[j]-x)+abs(a[k]-a[j])+abs(y-b[k])+2);
ans = min(ans,abs(a[j]-x)+abs(b[k]-b[j])+abs(y-a[k])+2);
ans = min(ans,abs(b[j]-x)+abs(b[k]-a[j])+abs(y-a[k])+2);
}
}
for(int j=1;j<=3;j++)
{
for(int k=1;k<=3;k++)
{
if(j==k)continue;
for(int t=1;t<=3;t++)
{
if(t==j||t==k)continue;
ans = min(ans,abs(a[j]-x)+abs(a[k]-b[j])+abs(a[t]-b[k])+abs(y-b[t])+3);
ans = min(ans,abs(b[j]-x)+abs(a[k]-a[j])+abs(a[t]-b[k])+abs(y-b[t])+3);
ans = min(ans,abs(a[j]-x)+abs(b[k]-b[j])+abs(a[t]-a[k])+abs(y-b[t])+3);
ans = min(ans,abs(b[j]-x)+abs(b[k]-a[j])+abs(a[t]-a[k])+abs(y-b[t])+3);
ans = min(ans,abs(a[j]-x)+abs(a[k]-b[j])+abs(b[t]-b[k])+abs(y-a[t])+3);
ans = min(ans,abs(b[j]-x)+abs(a[k]-a[j])+abs(b[t]-b[k])+abs(y-a[t])+3);
ans = min(ans,abs(a[j]-x)+abs(b[k]-b[j])+abs(b[t]-a[k])+abs(y-a[t])+3);
ans = min(ans,abs(b[j]-x)+abs(b[k]-a[j])+abs(b[t]-a[k])+abs(y-a[t])+3);
}
}
}
Ans = (Ans+1ll*i*ans)%mod;
}
printf("%lld\n",Ans);
}
}